const app = getApp()
import {
  getToken, toMb
} from '../../../utils/util.js'
import request from "../../../utils/request";
var api = require("../../../utils/api");

var timmer = null
var oldId = 0 // 旧文件数组最大id 
var isImg = (/.jpg$|.png$|.jpeg$|.gif$/i)
var isMv = (/.flv|.rvmb|.mp4|.avi|.wmv$/i)
var isDoc = (/.doc$|.xls$|.ppt$|.docx$|.xlsx$|.pptx$|.pdf$/i)
var isVoice = (/.rm|.mp3|.wma$/i)
var filePath = null
var longInputVal = ''
var sumTxtL = 1000
var textNowL = 0
var classId = 0
var isUpload = false
var imgStrArr = []

Page({
  data: {
    url: '',
    imgSize : 0, //总大小
    imgSumSize : 1024 * 20,
    imgArrs : [],
    imgsArr: [],
  },
  onLoad() {
    app.globalData.backPage = true
    this.checkParams()
  },
  checkParams() {
    var imgArr = decodeURI(app.globalData.sendWebFileArr)
    if (imgArr === '' || !imgArr) {
      return
    }
    var imgArr = JSON.parse(imgArr);
    //图片和文件分类
    var relArr = []
    var sumSize = 0
    if (imgArr.length <= 0) {
      alert('上传文件不能为空')
      return
    }
    for (let c = 0; c < imgArr.length; c++) {
      const item = imgArr[c];
      var obj = {}
      debugger
      if (isImg.test(item.imgPath)) {
        obj.file = item.imgPath
      } else {
        obj.file = ''
        obj.path = item.path
      }
      obj.size = item.size
      sumSize += item.size
      relArr.push(obj)
    }
    oldId = this.data.imgsArr.length
    this.setData({
      imgsArr: relArr,
      imgSize:  sumSize
    })
  },
  //提交
  upload(){
    var backObj = {
      file: '',
      size: '',
    }
    if (this.data.imgsArr.isNo) {
      app.globalData.fileArr = { isNos: false }
    } else {
      this.data.imgsArr.forEach(item =>{
        backObj.file +=   item.file + ','
        backObj.size += item.size + ","
      })
      backObj.file = backObj.file.substring(0, backObj.file.length - 1)
      backObj.size = backObj.size.substring(0, backObj.size.length - 1)
      backObj.up = 1
      app.globalData.fileArr = backObj
    }
    wx.navigateBack({
      delta: 1
    })
  },
  //删除
  onDel(e){
    var imgId = e.currentTarget.dataset.imgid
    var imgsArr = this.data.imgsArr
    var imgSize = Number(this.data.imgSize).toFixed(4)
    var itemSize = Number(this.data.imgsArr[imgId].size)
    imgSize = (imgSize - itemSize).toFixed(4)
    imgsArr.splice(imgId, 1)
    this.setData({
      imgsArr: imgsArr,
      imgSize: imgSize
    })
  },
  onUploadPage(){
    let self = this;
    var uploadUrl = api.SecretUpload_file
    var token = getToken()
    var openid = app.globalData.loginInfo.openid
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success: function (res) {
        wx.uploadFile({
          url: uploadUrl,
          filePath: res.tempFiles[0].path,
          name: 'file',
          formData: {
            'access_token': token,
            'size': toMb(res.tempFiles[0].size)
          },
          success: function (result) {
            var data = JSON.parse(result.data)
            if (data.code === 0) {
              let imgStrArr = self.data.imgsArr
              var size = Number(self.data.imgSize).toFixed(4)
              let itemSize = Number(data.data.size).toFixed(4)
              let imgSize = (Number(size) + Number(itemSize)).toFixed(4)
              data.data.size = Number(data.data.size).toFixed(4)
              imgStrArr.push(data.data)
              self.setData({
                imgStrArr: imgStrArr,
                imgsArr: imgStrArr,
                imgSize: imgSize
              })
            } else {

            }
          }
        })
      },
    })
  },

})